/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpdesk.ejb;

import com.helpdesk.entidades.HdTecnicos;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author Manuel
 */
@ManagedBean
@RequestScoped
public class MBReport {

    /**
     * Creates a new instance of MBReport
     */
    private HdTecnicos codTecnico;
    String user="root";
    String pass="root";

    public MBReport() {
    }

    public HdTecnicos getCodTecnico() {
        return codTecnico;
    }

    public void setCodTecnico(HdTecnicos codTecnico) {
        this.codTecnico = codTecnico;
    }
    
     public String generateReport()
            throws ClassNotFoundException, SQLException, IOException,
            JRException {
        Connection connection;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        java.io.InputStream reportStream = context.getExternalContext().getResourceAsStream("/Reportes/TcketsByTeci.jasper");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        Class.forName("com.mysql.jdbc.Driver");
        HashMap parametros = new HashMap();
        parametros.put("codTecnico", codTecnico.getCodTecnico());
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_helpdesk?user="+user+"&password="+pass);
        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parametros, connection);
        connection.close();
        response.setContentType("application/pdf");
        servletOutputStream.flush();
        servletOutputStream.close();
        return "";
    }
    
    
    public String reporteArea()
            throws ClassNotFoundException, SQLException, IOException,
            JRException {
        Connection connection;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        java.io.InputStream reportStream = context.getExternalContext().getResourceAsStream("/Reportes/TicketsByArea.jasper");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        Class.forName("com.mysql.jdbc.Driver");
       // HashMap parametros = new HashMap();
       // parametros.put("codTecnico", codTecnico.getCodTecnico());
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_helpdesk?user="+user+"&password="+pass);
        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), connection);
        connection.close();
        response.setContentType("application/pdf");
        servletOutputStream.flush();
        servletOutputStream.close();
        FacesContext.getCurrentInstance().responseComplete();
        return "";
    }


     public String reporteEstado()
            throws ClassNotFoundException, SQLException, IOException,
            JRException {
        Connection connection;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        java.io.InputStream reportStream = context.getExternalContext().getResourceAsStream("/Reportes/reportByEst.jasper");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        Class.forName("com.mysql.jdbc.Driver");
       // HashMap parametros = new HashMap();
       // parametros.put("codTecnico", codTecnico.getCodTecnico());
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_helpdesk?user="+user+"&password="+pass);
        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), connection);
        connection.close();
        response.setContentType("application/pdf");
        servletOutputStream.flush();
        servletOutputStream.close();
        FacesContext.getCurrentInstance().responseComplete();
        return "";
    }

      public String reporteDetallado()
            throws ClassNotFoundException, SQLException, IOException,
            JRException {
        Connection connection;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        java.io.InputStream reportStream = context.getExternalContext().getResourceAsStream("/Reportes/reportDetTicket.jasper");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        Class.forName("com.mysql.jdbc.Driver");
       // HashMap parametros = new HashMap();
       // parametros.put("codTecnico", codTecnico.getCodTecnico());
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_helpdesk?user="+user+"&password="+pass);
        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), connection);
        connection.close();
        response.setContentType("application/pdf");
        servletOutputStream.flush();
        servletOutputStream.close();
        FacesContext.getCurrentInstance().responseComplete();
        return "";
    }

      
       public String reportClasificaciones()
            throws ClassNotFoundException, SQLException, IOException,
            JRException {
        Connection connection;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        java.io.InputStream reportStream = context.getExternalContext().getResourceAsStream("/Reportes/reportByClasi.jasper");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        Class.forName("com.mysql.jdbc.Driver");
       // HashMap parametros = new HashMap();
       // parametros.put("codTecnico", codTecnico.getCodTecnico());
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_helpdesk?user="+user+"&password="+pass);
        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), connection);
        connection.close();
        response.setContentType("application/pdf");
        servletOutputStream.flush();
        servletOutputStream.close();
        FacesContext.getCurrentInstance().responseComplete();
        return "";
    }

}
